import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(AsyncRebuilderTestPage());
}

class AsyncRebuilderTestPage extends StatefulWidget {
  @override
  State createState() => _AsyncRebuilderTestPageState();
}

class _AsyncRebuilderTestPageState extends State<AsyncRebuilderTestPage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, widget) {
        return Center(
          child: StreamBuilder(
            stream: counter(),
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                return Text(
                  "Error: ${snapshot.error}",
                  style: TextStyle(
                    color: Colors.amber,
                    fontSize: 15,
                  ),
                );
              }
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text(
                    '没有Stream',
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
                case ConnectionState.waiting:
                  return Text(
                    '等待数据...',
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
                case ConnectionState.active:
                  return Text(
                    'active: ${snapshot.data}',
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
                case ConnectionState.done:
                  return Text(
                    'Stream已关闭',
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
              }
              return null;
            },
          ),
        );
      },
    );
  }

  Widget _futureBuilderWidget() {
    return MaterialApp(
      builder: (context, widget) {
        return Center(
          child: FutureBuilder(
            future: mockNetworkData(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return Text(
                    "Error: ${snapshot.error.toString()}",
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
                } else {
                  final content = snapshot.data as String;
                  return Text(
                    "content : $content",
                    style: TextStyle(
                      color: Colors.amber,
                      fontSize: 15,
                    ),
                  );
                }
              } else {
                return CircularProgressIndicator();
              }
            },
          ),
        );
      },
    );
  }
}

Future<String> mockNetworkData() async {
  return Future.delayed(Duration(seconds: 2), () => "网络延迟加载数据");
}

Stream<int> counter() {
  return Stream.periodic(Duration(seconds: 1), (i) {
    return i;
  });
}
